﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstOderSys : MonoBehaviour
{
    public ShuruType type = ShuruType.自由;
    public float a = 1.2f;//
    public float b0;
    public float a1;

    public float[] cn = new float[3] { 0, 0, 0 };
    public float[] rn = new float[2] { 0, 0 };
    public int n = 0;//当前的n值

    private float z;
    private float t;
    private float y;
    /// <summary>
    /// 初始化数据
    /// </summary>
    private void Awake()
    {

        n = 0;


        z = transform.position.z;
    }

    private void FixedUpdate()
    {
        b0 = Time.fixedDeltaTime * a / (1 + a * Time.fixedDeltaTime);
        a1 = -1 / (1 + a * Time.fixedDeltaTime);

        cn[0] = b0 * rn[0] - a1 * cn[1];
        cn[2] = cn[1];
        cn[1] = cn[0];
        rn[1] = rn[0];
        Shuru();
        n++;
        t = n * Time.fixedDeltaTime;
        y = cn[0];
        transform.position = new Vector3(t, y, z);
    }





    /// <summary>
    /// 阶跃函数
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    private void Shuru()
    {
        if (type == ShuruType.阶跃)
        {
            if (n >= 0)
            {
                rn[0] = 5;
                return;
            }
            rn[0] = 0;
        }
        if (type == ShuruType.斜坡)
        {
            if (n >= 0)
            {
                rn[0] = t;
                return;
            }
            rn[0] = 0;
        }



    }


    public enum ShuruType
    {
        阶跃,
        斜坡,
        自由
    }
}
